package com.gzhryc.common.exceptions;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class CheckErrorException extends Exception{

	private static final long serialVersionUID = 1L;
	
	private String message = "参数不正确";
	private Object entity;
	private LinkedHashMap<String,String> errors;
	
	public CheckErrorException(){}
	
	public CheckErrorException(String msg){
		this.message = msg;
	}

	public <T> T getEntity(Class<T> clazz) {
		try{
			return clazz.cast(entity);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public <T> void setEntity(T entity) {
		this.entity = entity;
	}
	
	public Map<String, String> getErrors() {
		if(errors == null){
			errors = new LinkedHashMap<String,String>();
		}
		return errors;
	}
	
	public void setErrors(LinkedHashMap<String, String> errors) {
		this.errors = errors;
	}

	
	@Override
	public String getMessage() {
		if(getErrors().size() > 0){
			return getErrors().values().iterator().next();
		}else{
			return message;
		}
	}

	
}
